package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.UserInfoVo;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import cn.wolfcode.luowowo.website.web.util.CookiesUtil;
import cn.wolfcode.luowowo.website.web.util.UMEditorUploader;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("")
public class SettingController {

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    //设置
    @RequireLogin//贴上这个需要登录后才能进入
    @RequestMapping("/mine/setting")
    public String setting(@UserParam UserInfo userInfo, UserInfoVo userInfoVo, Model model, HttpServletRequest request, HttpServletResponse response){
        System.out.println(userInfoVo.getDeliveryAddress());
        //我的设置里面的:我的信息,修改功能
        //出入当前登录用户和userInfoVo(我把前端传进入的数据封装到了userInfoVo里面)
        userInfoService.setUserInfoDataUp(userInfo,userInfoVo);
        //修改了数据后要更新redis里面的用户对象
        //获取token
        String token = CookiesUtil.getCookie(request, response, "token", RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
       //通过登录用户的id,获得最新的数据库里面的登录者的用户对象
        UserInfo user = userInfoService.get(userInfo.getId());
        //这个方法是更新redis
        userInfoRedisService.setUserLoginToken(token,user);
        //共享最新的用户数据
        model.addAttribute("user",user);

        return "mine/setting";
    }

//    //图片上传
//    @RequireLogin//贴上这个需要登录后才能进入
//    @RequestMapping("/upload/file")
//    @ResponseBody
//    public Object uploadUEImage(String files,MultipartFile upfile, HttpServletRequest request,@UserParam UserInfo userInfo ) throws Exception{
//
//    }

}
